window.addEventListener('devicemotion', deviceMotionHandler, false);
window.addEventListener('load',loadStorage,false);

var oNan = new ele('nan');
var oGua = new ele('gua');
var oGong = new ele('gong');
var oChe = new ele('che');
var oMain_door = new ele('main_door');
var oMain_card = new ele('main_card');
var oMain = new ele('main');
var oMain_action_rock = new ele('main_action_rock');
var oMain_action_git = new ele('main_action_git');
var arrEle = [oNan, oGua, oGong, oChe];
var arr = ['images/supernatant_card1.png', 'images/supernatant_card2.png', 'images/supernatant_card3.png', 'images/supernatant_card4.png'];
var num;
var oCover_surprise_card = new ele('cover_surprise_card');
var oMain_chance_num = new ele('main_chance_num');
var changeNum = 10;

//摇一摇
var lastX = null,
	lastY = null,
	lastZ = null;
var threshold = 3; //灵敏度（值越小灵敏度越高）
var timeout = 1000;
var lastTime = null;
var isShaking = !1;
lastTime = new Date();

function deviceMotionHandler(e) {
	var current = e.accelerationIncludingGravity;
	var currentTime;
	var timeDifference;
	var deltaX = 0;
	var deltaY = 0;
	var deltaZ = 0;

	//记录上一次设备在x,y,z方向上的加速度49             
	if((lastX === null) && (lastY === null) && (lastZ === null)) {
		lastX = current.
		lastY = current.y;
		lastZ = current.z;
		return;
	}

	//得到两次移动各个方向上的加速度绝对差距
	deltaX = Math.abs(lastX - current.x);
	deltaY = Math.abs(lastY - current.y);
	deltaZ = Math.abs(lastZ - current.z);
	//当差距大于设定的阀值并且时间间隔大于指定阀值时，触发摇一摇逻辑
	if(((deltaX > threshold) && (deltaY > threshold)) || ((deltaX > threshold) && (deltaZ > threshold)) || ((deltaY > threshold) && (deltaZ > threshold))) {
		currentTime = new Date;
		timeDifference = currentTime.getTime() - lastTime.getTime();
		//时间间隔
		if(timeDifference > timeout) {
			//触发摇一摇事件
			rock();
			lastTime = new Date;
		}
	}
	lastX = current.x;
	lastY = current.y;
	lastZ = current.z;
}


//摇出来的方法
function rock() {
	window.removeEventListener('devicemotion', deviceMotionHandler, !1);
	if(changeNum<=0){
		return false;
	}else{
		changeNum--;
	}
	oMain_chance_num.innerText = changeNum;
	num = Math.floor(Math.random() * 4);
	oCover_surprise_card.src = arr[num];
	ruleShow('cover_surprise');
	saveStorage();
}

function saveStorage() {
	var time = new Date().getTime();
	localStorage.setItem(time, num);
	loadStorage();
}

function loadStorage() {
	var arr = [0, 1, 2, 3];
	var nan = 0,
		gua = 0,
		gong = 0,
		che = 0;
	for(var i = 0; i < localStorage.length; i++) {
		var key = localStorage.key(i);
		var value = +localStorage.getItem(key);
		if(arr.indexOf(value) > -1) {
			arrEle[value].parentElement.classList.remove('hide');
			oMain_card.children[value].className = 'beijing';
			switch(value) {
				case 0:
					nan++;break;
				case 1:
					gua++;break;
				case 2:
					gong++;break;
				case 3:
					che++;break;
			}
		}
	}
	//有几张卡显示在页面上
	oNan.innerText = nan;
	oGua.innerText = gua;
	oGong.innerText = gong;
	oChe.innerText = che;
	if(nan == 0 || gua == 0 || gong == 0 || che == 0) {
		oMain_door.classList.remove('hide');
		oMain.classList.remove('mainB');
	} else {
		//		集齐
		window.removeEventListener('devicemotion', deviceMotionHandler, false);
		oMain_door.classList.add('hide');
		oMain.classList.add('mainB');
		oMain_action_rock.classList.add('none');
		oMain_action_git.classList.remove('none');
		var oMain_chance = new ele('main_chance');
		oMain_chance.innerText = '恭喜您点亮了南瓜公车获得10.31元现金红包';
		for(var i = 0; i < arrEle.length; i++){
			arrEle[i].parentElement.classList.add('hide');
		}
		
	}
}

//清除缓存
function clearStorage() {
	localStorage.clear();
}

//验证手机号
var oPhone = new ele('phone');
var pCover_phone_f = new ele('cover_phone_f');
var oCover_phone = new ele('cover_phone');
var oCover_hint_num = new ele('cover_hint_num');


//显示隐藏
function ruleShow(id) {
	document.getElementById(id).classList.toggle('hide');
}

//创建元素
function ele(id) {
	return document.getElementById(id);
}
//太棒了
function good() {
	ruleShow('cover_surprise');
	window.addEventListener('devicemotion', deviceMotionHandler, false);
	loadStorage();
	lastTime = new Date();
	return false;
}


oCover_phone.addEventListener('click', function(ev){
	var oEvent = ev.target.id || ev.srcElement.id;
	if(oEvent == 'cover_rule_know') {
		if(!(/^1[34578]\d{9}$/.test(oPhone.value))) {
			pCover_phone_f.classList.remove('hide');
			oPhone.focus();
			return false;
		} else {
			pCover_phone_f.classList.add('hide');
			oCover_phone.classList.add('hide');
			var phoneHide = oPhone.value.slice(0, 3) + "****" + oPhone.value.slice(-4);
			oCover_hint_num.innerText = phoneHide;
			oCover_phone.nextElementSibling.classList.remove('hide');
		}
	}else if(oEvent == 'phone'){
				oPhone.focus();
	}else{
			ruleShow('cover_phone');
		}
	
},false);